var upload = {
    error: function (msg) {
        common_ops.alert(msg)
    },
    success: function (file_key) {
        if (!file_key) {
            return;
        }
        var html = '<img style="overflow: hidden;margin: auto;height: 100px" src="' + common_ops.buildPicUrl(file_key) + '"/>' + '<span class="fa fa-times-circle del del_image" data="' + file_key + '"></span>';
        if ($('.upload_pic_wrap .pic-each').size() > 0) {
            $('.upload_pic_wrap .pic-each').html(html);
        } else {
            $('.upload_pic_wrap').append('<span class="pic-each">' + html + '</span>')
        }
        food_set_ops.delete_img();
    }
};

var food_set_ops = {
    init: function () {
        this.eventBind();
        this.initEditor();
        this.delete_img();
    },
    eventBind: function () {
        $('.save').click(function () {
            var select_i = $('select[name=cat_id]');
            var name_i = $('input[name=name]');
            var price_i = $('input[name=price]');
            var pic_i = $('.pic-each span');
            var stock_i = $('input[name=stock]');
            var tags_i = $('input[name=tags]');

            var cate_id = select_i.val();
            var name = name_i.val();
            var price = price_i.val();
            var pic = pic_i.attr('data');
            var stock = stock_i.val();
            var tags = tags_i.val();
            var desc = ueditor.getContent();
            var id = $(this).attr('data-id');

            if (cate_id == '0') {
                common_ops.tip('请先选择分类', select_i);
                return;
            }
            if (common_ops.isEmpty(name)) {
                common_ops.tip('请输入菜品名称', name_i);
                return;
            }
            if (common_ops.isEmpty(price)) {
                common_ops.tip('请输入价格', price_i);
                return;
            }
            if (common_ops.isEmpty(pic)) {
                common_ops.tip('请输入上传封面', pic_i);
                return;
            }
            if (common_ops.isEmpty(stock)) {
                common_ops.tip('请输入库存信息', stock_i);
                return;
            }
            if (common_ops.isEmpty(tags)) {
                common_ops.tip('请输入标签信息', tags_i);
                return;
            }

            $.ajax({
                url: '/food/set',
                type: 'POST',
                dataType: 'json',
                data: {
                    'name': name,
                    'price': price,
                    'pic': pic,
                    'cate_id': cate_id,
                    'stock': stock,
                    'tags': tags,
                    'desc': desc,
                    'id': id
                },
                success: function (data) {
                    if (data['code'] === 200) {
                        var callback = function () {
                            window.location.href = '/food/index';
                        };
                        common_ops.alert('菜品添加成功！', callback);
                    }
                    else {
                        common_ops.alert(data['msg']);
                    }
                },
                fail: function () {
                    common_ops.alert('数据提交失败请重试！');
                }
            });
        });
    },
    initEditor: function () {
        // 绑定select2搜索输入框的插件
        $('.wrap_food_set select[name=cat_id]').select2({
            language: 'zh-CN',
            width: '100%'
        });

        // 绑定标签输入框插件
        $('.wrap_food_set input[name=tags]').tagsInput({
            width: 'auto',
            height: 40,
            defaultText: '标签..'
        });

        //图片资源无刷新上传
        $('.wrap_food_set .upload_pic_wrap input[name=pic]').change(function () {
            $('.wrap_food_set .upload_pic_wrap').submit();
        });

        ueditor = UE.getEditor('container', {
            toolbars: [
                ['fullscreen', 'source', 'undo', 'redo'],
                ['bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', 'insertimage']
            ],
            enableAutoSave: true,
            saveInterval: 60000,
            elementPathEnabled: false,
            zIndex: 4,
            serverUrl: common_ops.buildUrl('/ueditorUpload/ueditor')
        });
        ueditor.ready(function () {
            var content = $('#container').attr('data-content');
            ueditor.setContent(content);
        });
    },
    delete_img: function () {
        $('.wrap_food_set .del_image').unbind().bind('click', function () {
            $(this).parent().remove();
        });
    }
};

$(function () {
    food_set_ops.init();
});